<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

/**
 * 系统权限控制器
 * @package App\Http\Controllers\Admin
 */
class SysPermissionController extends BaseController
{
    private $permSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        $this->permSer = new \App\Services\System\Permission();
    }

    /**
     * 首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $query = \App\Models\System\Permission::query();
        //搜索名称
        $name = $request->input('name');
        $name && $query->where('name','like',"%".$name."%");
        //搜索显示名
        $displayName = $request->input('display_name');
        $displayName && $query->where('display_name', 'like', "%".$displayName."%");
        $permList = $query->get();
        $permList = $permList->isNotEmpty() ? $permList->toArray() : [];
        if (empty($name) && empty($displayName)){
            //树结构用户组列表
            $tree          = new \App\Services\Tool\TreeService('perm_id', 'parent_id', 'children');
            $tree->load($permList);
            $treeMenuList = $tree->deepTree();
        }else{
            $treeMenuList = $permList;
        }
        $data['permList'] = $treeMenuList ? $treeMenuList : [];
        return $this->success('加载成功', $data);
    }
    /**
     * 保存、添加权限
     * @param Request $request
     * @return $this|\Illuminate\Http\Response
     */
    public function save(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'display_name' => 'required',
            'order_num' => 'integer|min:0',
        ]);
        try {
            $rs = $this->permSer->saveData($request->toArray());
            if ($rs) {
                return $this->success(($request->perm_id ? '编辑' : '添加') . '成功');
            }
            throw new \Exception(($request->perm_id ? '编辑' : '添加') . '权限失败');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
    /**
     * 删除
     * @return $this|\Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        try {
            $ids   = $request->perm_ids ?? [];
            if (!$ids){
                throw new \Exception('请指定要删除的权限');
            }
            $rs = $this->permSer->del($ids);
            if ($rs){
                return $this->success('删除成功');
            }
            throw new \Exception('删除失败');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * 保存排序数据
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setOrder(Request $request)
    {
        try {
            $draggingNode = $request->draggingNode ?? [];
            if (empty($draggingNode)){
                throw new \Exception('请指定拖拽节点');
            }
            $dropNode = $request->dropNode ?? [];
            if (empty($dropNode)){
                throw new \Exception('请指定被放置节点');
            }
            $dropType = $request->dropType ?? '';
            if (empty($dropType)){
                throw new \Exception('请指定拖拽类型');
            }
            $rs = $this->permSer->setOrder($draggingNode, $dropNode, $dropType);
            if ($rs){
                return $this->success('设置排序成功');
            }
            throw new \Exception('设置排序失败');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }
}
